//
//  KYZXVideoCollectionVC.m
//  KuYouZiXun
//
//  Created by lanou on 15/8/10.
//  Copyright (c) 2015年 lanou. All rights reserved.
//

#define kURL @"http://appapi.gamersky.com/v1/videocontentlist"

#import "KYZXVideoCollectionVC.h"

#import "LJRequestManagerBlock.h"

#import "KYZXVideoListModel.h"

#import "KYZXVideoCollectionViewCell.h"

#import "KYZXUserInfo.h"
@interface KYZXVideoCollectionVC ()


@end

@implementation KYZXVideoCollectionVC

static NSString * const reuseIdentifier = @"Cell";


- (void)dealloc{
    
    [[KYZXUserInfo standardKYZXUserInfo] removeObserver:self];
}

- (NSMutableArray *)dataSource{

    if (_dataSource == nil) {
        _dataSource = [[NSMutableArray alloc] initWithCapacity:0];
    }
    return _dataSource;
}


- (NSDictionary *)parameterDict{

    if (_parameterDict == nil) {
        _parameterDict = @{@"nodes":[self.nodeModel.nodeId stringValue], @"specials":@"" ,@"pageIndex":@"1",@"pageCount":@"20",@"tag":@""};
    }
    return _parameterDict;
}

#pragma mark 获取数据

- (void)requestData:(NSDictionary *)dic{

    [LJRequestManagerBlock manager:POST requestWith:kURL dic:dic finish:^(NSData *data) {
        
        NSDictionary *dataDict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers|NSJSONReadingMutableLeaves error:nil];
        NSArray *resultArray = dataDict[@"result"];
        for (NSDictionary *dic in resultArray) {
            KYZXVideoListModel *model = [[KYZXVideoListModel alloc] init];
            [model setValuesForKeysWithDictionary:dic];
            [self.dataSource addObject:model];
        }
        NSLog(@"%@数据获取完毕",self.nodeModel.nodeName);
        KYZXVideoListModel *model =self.dataSource[0];
        NSLog(@"model : %@",model.title);
        [self.collectionView reloadData];
    } error:^(NSError *error) {
        
    }];
}


- (void)viewDidLoad {
    
    [super viewDidLoad];
    [self.collectionView registerNib:[UINib nibWithNibName:@"KYZXVideoCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:reuseIdentifier];
    
    self.collectionView.backgroundColor = [KYZXUserInfo standardKYZXUserInfo].backgroundColor;
    
    [[KYZXUserInfo standardKYZXUserInfo]addObserver:self];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
   
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

#pragma mark <UICollectionViewDataSource>

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {

    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {

    return [self.dataSource count];
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    KYZXVideoListModel *model = self.dataSource[indexPath.item];
    KYZXVideoCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
    cell.model = model;
    return cell;
}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context{

    self.collectionView.backgroundColor = [KYZXUserInfo standardKYZXUserInfo].backgroundColor;
}

@end
